package com.tec.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.tec.security.entity.News;
import com.tec.security.mapper.NewsMapper;
import com.tec.security.service.NewsService;
import com.tec.security.utils.date.DateString;
import com.tec.security.utils.other.LimitPage;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author LuLin
* @description 针对表【News】的数据库操作Service实现
* @createDate 2023-10-21 09:22:50
*/
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper,News> implements NewsService{
    private final NewsMapper newsMapper;
    @Autowired
    public NewsServiceImpl(NewsMapper newsMapper) {
        this.newsMapper = newsMapper;
    }

    /**
     * @description 未审核新闻列表
     * @author 陆林
     * @date 2023-11-9
     */
    public Map<String, Object> listNOAudit(LimitPage limitPage) {
        Map<String,Object> map=new HashMap<>();;
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        List<News> newsList = newsMapper.selectList(queryWrapper);
        int total = newsList.size();
        PageHelper.startPage(limitPage.getPage(), limitPage.getSize());
        QueryWrapper<News> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.orderByDesc("id");
        List<News> pageNewsList = newsMapper.selectList(queryWrapper1);
        if (total > 0 && !pageNewsList.isEmpty()) {
            map.put("status",true);
            map.put("pageSize",pageNewsList);
            return map;
        } else {
            map.put("status",false);
            return map;
        }
    }

    /**
     * @description 分页新闻列表
     * @author 陆林
     * @date 2023-11-9
     */
    public Map<String,Object> pageList(LimitPage limitPage){
        QueryWrapper<News> NewsQueryWrapper=new QueryWrapper<>();
        int number= Math.toIntExact(newsMapper.selectCount(NewsQueryWrapper));
        QueryWrapper<News> queryWrapper1 = new QueryWrapper<>();
        PageHelper.startPage(limitPage.getPage(), limitPage.getSize());
        queryWrapper1.orderByDesc("id");
        Map<String,Object> map=new HashMap<>();
        map.put("total",number);
        map.put("list", newsMapper.selectList(queryWrapper1));
        return map;
    }


    /**
     * @description 更新文章
     * @author 陆林
     * @date 2023-11-27
     */
    public boolean repost(News News) {
        UpdateWrapper<News> NewsUpdateWrapper=new UpdateWrapper<>();
        NewsUpdateWrapper.eq("id",News.getId())
                .set("status",1)
                .set("updateTime", DateString.currentDate())
                .set("title",News.getTitle())
                .set("homeImageUrl",News.getHomeimageurl())
                .set("homeDescript",News.getHomedescript())
                .set("content",News.getContent());
        return newsMapper.update(null,NewsUpdateWrapper)>0;
    }

    /**
     * @description 文章浏览量
     * @author 陆林
     * @date 2023-11-27
     */
    public Map<String, Object> views(Integer id) {
        Map<String,Object> result=new HashMap<>();
        QueryWrapper<News> NewsQueryWrapper=new QueryWrapper<>();
        NewsQueryWrapper.eq("id",id);
        Integer number=newsMapper.selectOne(NewsQueryWrapper).getViews();
        number++;
        UpdateWrapper<News> newsUpdateWrapper=new UpdateWrapper<>();
        newsUpdateWrapper.eq("id",id).set("views",number);
        newsMapper.update(null,newsUpdateWrapper);
        result.put("views",number);
        return result;
    }

    /**
     * @description 通过文章审核
     * @author 陆林
     * @date 2023-11-27
     */
    public boolean newsAudit(Integer id,String name) {
        UpdateWrapper<News> NewsQueryWrapper=new UpdateWrapper<>();
        NewsQueryWrapper.eq("id",id).set("status",5).set("auditor",name);
        return newsMapper.update(null,NewsQueryWrapper)>0;
    }

    /**
     * @description 驳回文章审核
     * @author 陆林
     * @date 2023-11-27
     */
    public boolean newNoAudit(Integer id,String name){
        UpdateWrapper<News> NewsUpdateWrapper=new UpdateWrapper<>();
        NewsUpdateWrapper.eq("id",id)
                .set("status",2)
                .set("auditor",name);
        return newsMapper.update(null,NewsUpdateWrapper)>0;
    }

    /**
     * @description 当前登录者的新闻列表
     * @author 陆林
     * @date 2023-11-27
     */
    @Override
    public Map<String, Object> myNews(LimitPage limitPage, String name) {
        QueryWrapper<News> NewsQueryWrapper=new QueryWrapper<>();
        NewsQueryWrapper.eq("author",name);
        int number= Math.toIntExact(newsMapper.selectCount(NewsQueryWrapper));
        QueryWrapper<News> queryWrapper1 = new QueryWrapper<>();
        PageHelper.startPage(limitPage.getPage(), limitPage.getSize());
        queryWrapper1.orderByDesc("inserttime").eq("author",name);
        Map<String,Object> map=new HashMap<>();
        map.put("total",number);
        map.put("list", newsMapper.selectList(queryWrapper1));
        return map;
    }

    /**
     * @description 删除文章ServiceImpl
     * @author 陆林
     * @date 2023-11-27
     */
    public Map<String,Object> myNews(String name){
        Map<String,Object> map=new HashMap<>();
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("author",name);
        List<News> newsList = newsMapper.selectList(queryWrapper);
        map.put("status",true);
        map.put("list",newsList);
        return map;
    }

    /**
     * @description 新建新闻
     * @author 陆林
     * @date 2023-11-27
     */
    public boolean insertNews(News news) {
        news.setStatus(1);
        news.setViews(0);
        int insertNumber = newsMapper.insert(news);
        return insertNumber >= 0;
    }

    /**
     * @description 删除文章ServiceImpl
     * @author 陆林
     * @date 2023-11-27
     */
    public boolean deletedNews(int id){
        int _temp= newsMapper.deleteById(id);
        return _temp>0;
    }

    /**
     * @description id获取单篇新闻
     * @author 陆林
     * @date 2023-11-27
     */
    public Map<String, Object> NewsSelectOneByID(int id) {
        QueryWrapper<News> NewsQueryWrapper=new QueryWrapper<>();
        NewsQueryWrapper.eq("id",id);
        News News= newsMapper.selectOne(NewsQueryWrapper);
        Map<String,Object> result=new HashMap<>();
        result.put("news",News);
        return result;
    }

    /**
     * @description 获取最新三篇新闻
     * @author 陆林
     * @date 2023-11-27
     */
    public Map<String, Object> newsSelectThree_Home() {
        QueryWrapper<News> NewsQueryWrapper=new QueryWrapper<>();
        NewsQueryWrapper.orderByDesc("inserttime").last("limit 3").eq("status",5);
        List<News> _temp= newsMapper.selectList(NewsQueryWrapper);
        Map<String,Object> result=new HashMap<>();
        result.put("newsdata",_temp);
        return result;
    }

    /**
     * @description 已经通过审核的新闻列表
     * @author 陆林
     * @date 2023-11-27
     */
    public Map<String, Object> newsListAudit() {
        @Data
        class Temp{
            private int id;
            private String title;
            private String date;
        }
        Map<String,Object> result=new HashMap<>();
        QueryWrapper<News> NewsQueryWrapper=new QueryWrapper<>();
        NewsQueryWrapper.eq("status",5).orderByDesc("inserttime");
        List<News> list= newsMapper.selectList(NewsQueryWrapper);
        List<Temp> _temp=new ArrayList<>();
        for (News i:list) {
            Temp temp=new Temp();
            temp.title=i.getTitle();
            temp.id=i.getId();
            temp.date=i.getInserttime().split(" ")[0];
            _temp.add(temp);
        }
        result.put("list",_temp);
        return result;
    }

    @Override
    public boolean newsEnable(Integer id, String name) {
        UpdateWrapper<News> newsUpdateWrapper=new UpdateWrapper<>();
        newsUpdateWrapper.eq("id",id).set("status",5).set("author",name);
        return newsMapper.update(null, newsUpdateWrapper) > 0;
    }

    @Override
    public boolean newsDeactivated(Integer id, String name) {
        UpdateWrapper<News> newsUpdateWrapper=new UpdateWrapper<>();
        newsUpdateWrapper.eq("id",id).set("status",6).set("author",name);
        return newsMapper.update(null, newsUpdateWrapper) > 0;
    }
}




